rm(list=ls())

library(survey)
library(foreign)
library(doBy)
library(xtable)
library(ggplot2)

cces <- read.dta("CCES_2010b.dta")
cces[cces == "NA"] <- NA


## Create variable with self-report of which party they voted for in the primary
## 1=democrat, 0=republican
cces$presprimary_party<-NA
cces$presprimary_party[cces$cc325a==1 |cces$cc325a==2 | cces$cc325a==3 |cces$cc325a==4] <-1
cces$presprimary_party[cces$cc325b==1 |cces$cc325b==2 | cces$cc325b==3 |cces$cc325b==4] <-1
cces$presprimary_party[cces$cc325a==5 |cces$cc325a==6 | cces$cc325a==7 |cces$cc325a==8 |cces$cc325a==9 |cces$cc325b==10] <-0
cces$presprimary_party[cces$cc325a==5 |cces$cc325b==6 | cces$cc325b==7 |cces$cc325b==8 |cces$cc325b==9 |cces$cc325b==10] <-0

cces$presgeneral_party<-NA
cces$presgeneral_party[cces$congress_post_vote==1] <-"Democrat"
cces$presgeneral_party[cces$congress_post_vote==0] <-"Republican"


primary_type_dems <- read.csv("Primary Dates and Types 2008-2014 - Dems.csv",colClasses="character")
primary_type_reps <- read.csv("Primary Dates and Types 2008-2014 - Reps.csv",colClasses="character")

primary_type<-as.data.frame(cbind(primary_type_dems$abb,  primary_type_dems$X2008.President.Type, primary_type_reps$X2008.President.Type))

# merge the data
cces10 <- cbind(primary_type[match(cces$abb,primary_type$abb),],
              cces)


# normalize ideology estimates
cces10$est <- (cces10$est - mean(cces10$est,na.rm=T))/sd(cces10$est)

# scap4 = retrospective primary choice (september)
# bcap4 = prospective primary choice (baseline)
# different primary dates...
# p = post, j = january, m = march, o=october
# e2012p = validated primaryturnout & registration
cces10$primaryturnout <- NA
# first, use known primary primaryturnout and self reports of *which* primary
cces10$primaryturnout[cces10$congress_post_vote=="0" & cces10$voted_primary_validated == "1" ] <- "Republican"
cces10$primaryturnout[cces10$congress_post_vote=="1" & cces10$voted_primary_validated == "1" ] <- "Democrat"


## Tidy up the data first
library(survey)

cces10$pid7<-as.numeric(as.vector(cces10$pid7))


cces10$voted_primary_validated[is.na(cces10$voted_primary_validated) & !is.na(cces10$congress_post_vote)]<-0

cces10$general_noprimary[cces10$voted_general_validated==1 & cces10$voted_primary_validated==0]<-1
cces10$general_noprimary[is.na(cces10$general_noprimary) & !is.na(cces10$congress_post_vote)]<-0

cces10$gender<-as.numeric(as.vector(cces10$gender))
cces10$idealpoint<-as.numeric(as.vector(cces10$est))
cces10$self_ideology<-as.numeric(as.vector(cces10$self_ideology))
cces10$donor<-as.numeric(as.vector(cces10$donor))
cces10$political_interest2<- as.numeric(as.vector(cces10$political_interest2))
cces10$hs[cces10$education==2]<-1
cces10$hs[cces10$education!=2]<-0

cces10$pol_interest[cces10$political_interest2==1]<-1
cces10$pol_interest[cces10$political_interest2!=1]<-0

cces10$college[cces10$education>=4]<-1
cces10$college[cces10$education<4]<-0

cces10$white[cces10$race==1]<-1
cces10$white[cces10$race!=1]<-0

cces10<-subset(cces10, !is.na(weight) )


cces10$weight<-as.numeric(as.vector(cces10$weight))
cces10.design<-svydesign(ids=cces10$respondent, data = cces10, weights=cces10$weight)

cces10_primary<-subset(cces10,primaryturnout!="0")
cces10_primary.design<-svydesign(ids=cces10_primary$respondent, data = cces10_primary, weights=cces10_primary$weight)

cces10_general<-subset(cces10,cces10$voted_general_validated==1 )
cces10_general.design<-svydesign(ids=cces10_general$respondent, data = cces10_general, weights=cces10_general$weight)


cces10_general_noprim<-subset(cces10,(voted_general_validated==1 & voted_primary_validated!=1))
cces10_general_noprim.design<-svydesign(ids=cces10_general_noprim$respondent, data = cces10_general_noprim, weights=cces10_general_noprim$weight)



## cross-tabs of who voted
# by party

cces10$voted_primary_validated<-as.numeric(as.vector(cces10$voted_primary_validated))
cces10$general_noprimary<-as.numeric(as.vector(cces10$general_noprimary))
cces10$voted_primary_validated<-as.numeric(as.vector(cces10$voted_primary_validated))
cces10$general_noprimary<-as.numeric(as.vector(cces10$general_noprimary))


svymean(cces10$voted_primary_validated[cces10$pid7<4], design=cces10.design[cces10$pid7<4], na.rm=T)
svymean(cces10$general_noprimary[cces10$pid7<4], design=cces10.design[cces10$pid7<4], na.rm=T)

svymean(cces10$voted_primary_validated[cces10$pid7>4], design=cces10.design[cces10$pid7>4], na.rm=T)
svymean(cces10$general_noprimary[cces10$pid7>4], design=cces10.design[cces10$pid7>4], na.rm=T)

## make density graph of ideal points
mean_idealpoint_primary_dems <- svymean(cces10_primary$idealpoint[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)

mean_idealpoint_partyfollowing_dems <- svymean(cces10_general$idealpoint[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

mean_idealpoint_primary_reps <- svymean(cces10_primary$idealpoint[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)

mean_idealpoint_partyfollowing_reps <- svymean(cces10_general$idealpoint[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

cces10$Party<-cces10$pid3
cces10$Party[cces10$pid3==2]<-NA

pdf('density_cces10_congBW.pdf', height = 4, width = 7)
ggplot(subset(cces10, !is.na(Party)), aes(idealpoint, fill = Party)) +
geom_density(alpha = 0.1)+ scale_fill_manual(breaks = c("1", "3"),
                        values=c("grey",  "black"),labels=c("Democrats", "Republicans"),guide = FALSE)+
    geom_vline(xintercept = mean_idealpoint_primary_dems[1], linetype="longdash")+
     geom_vline(xintercept = mean_idealpoint_partyfollowing_dems[1])+
    geom_vline(xintercept = mean_idealpoint_primary_reps[1], linetype="longdash")+
    geom_vline(xintercept = mean_idealpoint_partyfollowing_reps[1])+
      xlab("Ideal Point") +
     annotate("text",label="Democrats",x=-1.2,y=.3,size=4)+
       annotate("text",label="Republicans",x=1.2,y=.3,size=4)+
        theme_bw() 
dev.off()


fulltable <- NULL

## age
col1<-svyquantile(~cces10_primary$age_exact[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T, quantile=.5)
col2<-svyquantile(~cces10_general_noprim$age_exact[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T, quantile=.5)
col3<-svyquantile(~cces10_general$age_exact[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T, quantile=.5)

col4<-svyquantile(~cces10_primary$age_exact[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T, quantile=.5)
col5<-svyquantile(~cces10_general_noprim$age_exact[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T, quantile=.5)
col6<-svyquantile(~cces10_general$age_exact[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T, quantile=.5)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "age"; fulltable <- rbind(fulltable,mat)

## hs degree
col1<-svymean(cces10_primary$hs[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$hs[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$hs[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$hs[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$hs[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$hs[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "hs degree"; fulltable <- rbind(fulltable,mat)

## college degree
col1<-svymean(cces10_primary$college[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$college[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$college[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$college[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$college[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$college[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "college degree"; fulltable <- rbind(fulltable,mat)

## gender
col1<-svymean(cces10_primary$gender[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$gender[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$gender[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$gender[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$gender[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$gender[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "gender"; fulltable <- rbind(fulltable,mat)

## white
col1<-svymean(cces10_primary$white[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$white[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$white[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$white[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$white[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$white[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "white"; fulltable <- rbind(fulltable,mat)

## donated to a candidate
col1<-svymean(cces10_primary$donor[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$donor[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$donor[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)
diff<-col1-col2
z_test1 = (col1-col2)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col2)$SE^2)

col4<-svymean(cces10_primary$donor[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$donor[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$donor[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)
z_test2 = (col4-col5)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col5)$SE^2)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "donated"; fulltable <- rbind(fulltable,mat)

## very interested in politics
col1<-svymean(cces10_primary$pol_interest[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$pol_interest[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$pol_interest[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$pol_interest[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$pol_interest[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$pol_interest[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "very interested"; fulltable <- rbind(fulltable,mat)


## ideological self-placement
col1<-svymean(cces10_primary$self_ideology[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$self_ideology[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$self_ideology[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$self_ideology[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$self_ideology[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$self_ideology[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "self-placement"; fulltable <- rbind(fulltable,mat)


## Issue ideology
##CCES
col1<-svymean(cces10_primary$idealpoint[cces10_primary$primaryturnout=="Democrat" & cces10_primary$source=="CCES_2010"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat" & cces10_primary$source=="CCES_2010"], na.rm=T)
col2<-svymean(cces10_general_noprim$idealpoint[cces10_general_noprim$presgeneral_party=="Democrat" & cces10_general_noprim$source=="CCES_2010"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat" & cces10_general_noprim$source=="CCES_2010"], na.rm=T)
col3<-svymean(cces10_general$idealpoint[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4 & cces10_general$source=="CCES_2010"], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4 & cces10_general$source=="CCES_2010"], na.rm=T)

col4<-svymean(cces10_primary$idealpoint[cces10_primary$primaryturnout=="Republican" & cces10_primary$source=="CCES_2010"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican" & cces10_primary$source=="CCES_2010"], na.rm=T)
col5<-svymean(cces10_general_noprim$idealpoint[cces10_general_noprim$presgeneral_party=="Republican" & cces10_general_noprim$source=="CCES_2010"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican" & cces10_general_noprim$source=="CCES_2010"], na.rm=T)
col6<-svymean(cces10_general$idealpoint[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4 & cces10_general$source=="CCES_2010"], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4 & cces10_general$source=="CCES_2010"], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "ideology"; fulltable <- rbind(fulltable,mat)

##########
## Issues
##########


# Abortion
col1<-svymean(cces10_primary$cces2006_abortion[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$cces2006_abortion[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$cces2006_abortion[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$cces2006_abortion[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$cces2006_abortion[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$cces2006_abortion[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "abortion"; fulltable <- rbind(fulltable,mat)

# Health Reform 
col1<-svymean(cces10_primary$cces2010_healthreform[cces10_primary$primaryturnout=="Democrat"], design=cces10_primary.design[cces10_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(cces10_general_noprim$cces2010_healthreform[cces10_general_noprim$presgeneral_party=="Democrat"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(cces10_general$cces2010_healthreform[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], design=cces10_general.design[cces10_general$presgeneral_party=="Democrat" | cces10_general$pid7<4], na.rm=T)

col4<-svymean(cces10_primary$cces2010_healthreform[cces10_primary$primaryturnout=="Republican"], design=cces10_primary.design[cces10_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(cces10_general_noprim$cces2010_healthreform[cces10_general_noprim$presgeneral_party=="Republican"], design=cces10_general_noprim.design[cces10_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(cces10_general$cces2010_healthreform[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], design=cces10_general.design[cces10_general$presgeneral_party=="Republican" | cces10_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "health reform"; fulltable <- rbind(fulltable,mat)


fulltable
xtab <- print(xtable(fulltable))
cat(xtab,file="cces2010.tex")


